//
//  ViewController.m
//  base
//
//  Created by 吕超 on 17/3/7.
//  Copyright © 2017年 lc. All rights reserved.
//

#import "ViewController.h"
#import "expertsLibraryTVCell.h"
#import "Tools.h"
#import "expertsLibraryModel.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
//#import "BaseTableViewCell.h"
//#import "tableCell.h"


@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

//  数据数组
@property (nonatomic, strong) NSMutableArray *dataArray;

//  上拉 下拉   下标
@property (nonatomic, assign)NSInteger newsIndex;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
//#pragma mark =====  get 请求
//    [self GetRequsetDataUrlString:@"http://app.51zzj.com.cn/api/user/index/initFlag.htm" Parameters:nil];
//    self.GetSuccess=^(id jsonObject){
//        NSLog(@"get ：%@",jsonObject);
//    };
//
//    
//#pragma mark =====  post 请求
//    NSDictionary *parameters = @{@"token": @"1d68740c62ba426985ad500c69eb818d" , @"questionId":@"QUS00000013" , @"condition" : @"Dwawawawdadaadwaw" , @"userId":@"USR00000148"};
//    //
//    [super PostRequsetDataUrlString:@"http://app.51zzj.com.cn/api/question/detail/reply.htm" Parameters:parameters];
//    self.PostSuccess=^(id jsonObject){
//                NSLog(@"get ：%@",jsonObject);
//    };
    
    
    
    // 初始值  通过修改该数组来增加请求数
    self.newsIndex = 1;
    
    //  添加  下拉  上拉
    [self createMJRefresh];
    //  网络请求
    [self MJRequestData];
    
    // 防止 block  循环 引用
    __weak typeof(self) WeakSelf = self;
    
    //  下拉 实现
    self.header = ^{
        [WeakSelf MJRequestData];
    };
    
    // 上拉  实现
    self.footer = ^{
        [WeakSelf MJRequestData];
    };
    
    
    //  添加 主tableView
    self.mainTableView.rowHeight = 125;
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    // 注册 重用名
    [self.mainTableView registerClass:[expertsLibraryTVCell class] forCellReuseIdentifier:@"expertsLibraryTVCell"];
    [self.view addSubview: self.mainTableView];
}



//  mj 请求数据
- (void)MJRequestData{
    if (self.judge == 10) {
        // 下拉时 清空数据数组 重新请求添加
        self.newsIndex = 1;
        [self.dataArray removeAllObjects];
    } else if (self.judge == 11){
        // 上拉时  数组中添加内容
        self.newsIndex++;
    }
         __weak typeof(self) WeakSelf = self;
    
    //转换一下
    NSString *Number = [NSString stringWithFormat:@"%ld",(long)self.newsIndex];
    
    NSDictionary *parameters = @{@"pageNum": Number, @"pageSize":@"10",@"content" : @""};
    
    //网络请求
    NSString *urlString = [Tools RequestnetworkString:@"expert/list/init.htm"];
    
    [super PostRequsetDataUrlString:urlString Parameters:parameters];
    self.PostSuccess=^(id responseObject){
        //    解析出来的关键字
        NSArray *rootArray = responseObject[@"businessData"];
        
        for (NSDictionary *smallDict in rootArray) {
            
            expertsLibraryModel *model = [[expertsLibraryModel alloc] init];
            
            [model setValuesForKeysWithDictionary:smallDict];
            
            [WeakSelf.dataArray addObject:model];
            
        }
 
        //刷新数据
        [WeakSelf.mainTableView reloadData];
        
        [WeakSelf.mainTableView.mj_header endRefreshing];
        
        if (rootArray.count == 0) {
            // 为零时 实现没有更多数据
            [WeakSelf.mainTableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [WeakSelf.mainTableView.mj_footer endRefreshing];
        }
    };
}



//  数组个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}


#pragma mark ==== 数据源
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    expertsLibraryTVCell *cell = [tableView dequeueReusableCellWithIdentifier:@"expertsLibraryTVCell"];
    
    // 重用
    if(cell == nil){
        cell = [[expertsLibraryTVCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"expertsLibraryTVCell"];
    }
    
    
    
    expertsLibraryModel *model = self.dataArray[indexPath.row];
    
    // 专家 推荐语
    cell.contentLabel.text = model.summary;
    
    
    // 专家名称
    cell.NameLabel.text = model.title;
    
    //下载图片  引用第三方
    
    [cell.headView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl] placeholderImage:[UIImage imageNamed:@"img_default"]];
    
    return cell;
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}


//- (UITableView *)mainTableView{
//
//
//
//}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
